package com.breeze.jetpackproject.viewmodel;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

public class Demo2ViewModel extends ViewModel {
    private MutableLiveData<String> nameLiveData;
    private final Demo2Model demo2Model;

    public Demo2ViewModel() {
        demo2Model = new Demo2Model();
    }

    public LiveData<String> getName() {
        if (nameLiveData == null) {
            nameLiveData = new MutableLiveData<>();
            loadName();
        }
        return nameLiveData;
    }

    private void loadName() {
        new Thread() {
            @Override
            public void run() {
                super.run();
                demo2Model.loadName(new AccountCallback() {
                    @Override
                    public void onLoadCompleted(String name) {
                        nameLiveData.postValue(name);
                    }
                });
            }
        }.start();
    }
}
